//package cn.mvp_frame.utils;
//
//import android.Manifest;
//import android.app.Activity;
//import android.app.Dialog;
//import android.content.DialogInterface;
//import android.content.Intent;
//import android.content.pm.PackageManager;
//import android.net.Uri;
//import android.os.Build;
//import android.provider.Settings;
//import android.support.annotation.NonNull;
//import android.support.v4.app.ActivityCompat;
//import android.support.v7.app.AlertDialog;
//import android.text.TextUtils;
//import android.util.Log;
//import android.widget.Toast;
//
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import cn.mvp_frame.R;
//
//
//public class PermissionUtils {
//    private static final String TAG = PermissionUtils.class.getSimpleName();
//    public static final int CODE_RECORD_AUDIO = 0;
//    public static final int CODE_GET_ACCOUNTS = 1;
//    public static final int CODE_READ_PHONE_STATE = 2;
//    public static final int CODE_CALL_PHONE = 3;
//    public static final int CODE_CAMERA = 4;
//    public static final int CODE_ACCESS_FINE_LOCATION = 5;
//    public static final int CODE_ACCESS_COARSE_LOCATION = 6;
//    public static final int CODE_READ_EXTERNAL_STORAGE = 7;
//    public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
//    public static final int CODE_MULTI_PERMISSION = 100;
//    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
//    public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
//    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
//    public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
//    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
//    public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission
//            .ACCESS_FINE_LOCATION;
//    public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission
//            .ACCESS_COARSE_LOCATION;
//    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission
//            .READ_EXTERNAL_STORAGE;
//    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission
//            .WRITE_EXTERNAL_STORAGE;
//    private static final String[] requestPermissions = {PERMISSION_RECORD_AUDIO,
//            PERMISSION_GET_ACCOUNTS, PERMISSION_READ_PHONE_STATE, PERMISSION_CALL_PHONE,
//            PERMISSION_CAMERA, PERMISSION_ACCESS_FINE_LOCATION,
//            PERMISSION_ACCESS_COARSE_LOCATION, PERMISSION_READ_EXTERNAL_STORAGE,
//            PERMISSION_WRITE_EXTERNAL_STORAGE};
//
//    /**
//     * 授权成功回调
//     */
//    public interface PermissionGrant {
//        void onPermissionGranted(int requestCode);
//    }
//
//    /**
//     * 获取权限
//     *
//     * @param activity
//     * @param requestCode     权限标识  CODE_RECORD_AUDIO等
//     * @param permissionGrant 权限已成功授予回调
//     */
//    public static void requestPermission(final Activity activity, final int requestCode,
//                                         PermissionGrant permissionGrant) {
//        if (activity == null) {
//            return;
//        }
////        判断权限标识是否有对应的权限
//        if (requestCode < 0 || requestCode >= requestPermissions.length) {
//            return;
//        }
//        final String requestPermission = requestPermissions[requestCode];
//        //如果是6.0以下的手机，ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED，
//        // 但是，如果用户关闭了你申请的权限，ActivityCompat.checkSelfPermission(),会导致程序崩溃(java.lang
//        // .RuntimeException: Unknown exception code: 1 msg null)，
//        // 你可以使用try{}catch(){},处理异常，也可以在这个地方，低于23就什么都不做，
//        // 个人建议try{}catch(){}单独处理，提示用户开启权限。
////        android 6.0以下
//
//
//
////        if (Build.VERSION.SDK_INT < 23) {
////            开启权限设置提示框
////            openSettingActivity(activity, getDialogContent(activity, new int[]{requestCode}));
////            return;
////        }
//
//        int checkSelfPermission;
//        try {
//            checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
//        } catch (RuntimeException e) {
//            //        没有该权限
//            openSettingActivity(activity, getDialogContent(activity, new int[]{requestCode}));
//            return;
//        }
//
////        判断是否已允许
//        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
////            shouldShowRequestPermissionRationale是否禁止弹出软件请求框
//            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
// requestPermission)) {
////                禁止
//                shouldShowRationale(activity, requestCode, requestPermission);
//            } else {
//                ActivityCompat.requestPermissions(activity, new String[]{requestPermission},
//                        requestCode);
//            }
//        } else {
////            允许
//            permissionGrant.onPermissionGranted(requestCode);
//        }
//    }
//
//    private static void requestMultiResult(Activity activity, String[] permissions, int[]
//            grantResults, PermissionGrant permissionGrant) {
//
//        if (activity == null) {
//            return;
//        }
//        Map<String, Integer> perms = new HashMap<>();
//
//        ArrayList<String> notGranted = new ArrayList<>();
//        for (int i = 0; i < permissions.length; i++) {
//            perms.put(permissions[i], grantResults[i]);
//            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
//                notGranted.add(permissions[i]);
//            }
//        }
//
//        if (notGranted.size() == 0) {
//            permissionGrant.onPermissionGranted(CODE_MULTI_PERMISSION);
//        } else {
//            int[] requestCodes = new int[notGranted.size()];
//            for (String requestCode : notGranted) {
//
//            }
//            openSettingActivity(activity, activity.getResources().getString(R.string
//                    .OPEN_PERMISSION));
//        }
//
//    }
//
//
//    /**
//     * 申请全部权限
//     */
//    public static void requestMultiPermissions(final Activity activity, PermissionGrant grant) {
//        //如果是6.0以下的手机，ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED，
//        // 但是，如果用户关闭了你申请的权限，ActivityCompat.checkSelfPermission(),会导致程序崩溃(java.lang
//        // .RuntimeException: Unknown exception code: 1 msg null)，
//        // 你可以使用try{}catch(){},处理异常，也可以在这个地方，低于23就什么都不做，
//        // 个人建议try{}catch(){}单独处理，提示用户开启权限。
////        android 6.0以下
//
//
//
//
////        if (Build.VERSION.SDK_INT < 23) {
////            开启权限设置提示框
////            openSettingActivity(activity, activity.getResources().getString(R.string
////                    .OPEN_PERMISSION));
////            return;
////        }
//        final List<String> permissionsList = getNoGrantedPermission(activity, false);
// 未禁止弹出框、未获取的权限
//        final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity,
//                true);//禁止弹出框的未获取的权限
//
//        if (permissionsList == null || shouldRationalePermissionsList == null) {
//            return;
//        }
//        if (permissionsList.size() > 0) {
//            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new
//                    String[permissionsList.size()]), CODE_MULTI_PERMISSION);
//        } else if (shouldRationalePermissionsList.size() > 0) {
//            showMessageOKCancel(activity, activity.getResources().getString(R.string
//                    .OPEN_PERMISSION), new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList
//                            .toArray(new String[shouldRationalePermissionsList.size()]),
//                            CODE_MULTI_PERMISSION);
//                }
//            });
//        } else {
//            grant.onPermissionGranted(CODE_MULTI_PERMISSION);
//        }
//
//    }
//
//    private static void shouldShowRationale(final Activity activity, final int requestCode, final
//    String requestPermission) {
//        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
//        showMessageOKCancel(activity, getDialogContent(activity, new int[]{requestCode}), new
//                DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                ActivityCompat.requestPermissions(activity, new String[]{requestPermission},
//                        requestCode);
//            }
//        });
//    }
//
//    private static void showMessageOKCancel(final Activity context, String message,
//                                            DialogInterface.OnClickListener okListener) {
//        new AlertDialog.Builder(context).setMessage(message).setPositiveButton(R.string.confirm,
//                okListener).setNegativeButton(R.string.cancel, null).create().show();
//    }
//
//    /**
//     * activity------ onRequestPermissionsResult   方法回调处调用
//     * 结果回调
//     *
//     * @param activity
//     * @param requestCode  Need consistent with requestPermission
//     * @param permissions
//     * @param grantResults
//     */
//    public static void requestPermissionsResult(final Activity activity, final int requestCode,
//                                                @NonNull String[] permissions, @NonNull int[]
//                                                        grantResults, PermissionGrant
//                                                        permissionGrant) {
//        if (activity == null) {
//            return;
//        }
//        if (requestCode == CODE_MULTI_PERMISSION) {
//            requestMultiResult(activity, permissions, grantResults, permissionGrant);
//            return;
//        }
//
//        if (requestCode < 0 || requestCode >= requestPermissions.length) {
//            return;
//        }
//
//        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//            permissionGrant.onPermissionGranted(requestCode);
//
//        } else {
//            String[] permissionsHint = activity.getResources().getStringArray(R.array
// .permissions);
//            openSettingActivity(activity, getDialogContent(activity, new int[]{requestCode}));
//        }
//    }
//
//    /**
//     * 打开应用权限是指界面
//     *
//     * @param activity
//     * @param message
//     */
//    private static void openSettingActivity(final Activity activity, String message) {
//        new AlertDialog.Builder(activity).setMessage(message).setPositiveButton(R.string.setting,
//                new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                Intent intent = new Intent();
//                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
//                intent.setData(uri);
//                activity.startActivity(intent);
//            }
//        }).setNegativeButton(R.string.confirm, null).create().show();
//    }
//
//    /**
//     * @param activity
//     * @param isShouldRationale true :返回   禁止弹出允许框的、未获取的权限
//     *                          false：返回   未获取的权限
//     * @return
//     */
//    public static ArrayList<String> getNoGrantedPermission(Activity activity, boolean
//            isShouldRationale) {
//
//        ArrayList<String> permissions = new ArrayList<>();
//
//        for (int i = 0; i < requestPermissions.length; i++) {
//            String requestPermission = requestPermissions[i];
//            int checkSelfPermission = -1;
//            try {
//                checkSelfPermission = ActivityCompat.checkSelfPermission(activity,
//                        requestPermission);
//            } catch (RuntimeException e) {
////            开启权限设置提示框
//                openSettingActivity(activity, getDialogContent(activity, new int[]{i}));
//                return null;
//            }
//
//            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
//
//                if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
//                        requestPermission)) {
//                    if (isShouldRationale) {
//                        permissions.add(requestPermission);
//                    }
//
//                } else {
//                    if (!isShouldRationale) {
//                        permissions.add(requestPermission);
//                    }
//                }
//
//            }
//        }
//
//        return permissions;
//    }
//
//    public static String getDialogContent(Activity activity, int[] requestCode) {
//        if (requestCode == null || requestCode.length == 0) return "";
//        String content = activity.getResources().getString(R.string.OPEN_PERMISSIONS);
//        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
//        for (int r : requestCode) {
//            switch (r) {
//                case CODE_RECORD_AUDIO:
////                录音
//                    content = content + permissionsHint[0];
//                    break;
//                case CODE_GET_ACCOUNTS:
////                访问GMail账户列表
//                    content = content + permissionsHint[1];
//                    break;
//                case CODE_READ_PHONE_STATE:
////                读取手机状态的权限
//                    content = content + permissionsHint[2];
//                    break;
//                case CODE_CALL_PHONE:
////                拨打电话
//                    content = content + permissionsHint[3];
//                    break;
//                case CODE_CAMERA:
////                访问使用照相设备
//                    content = content + permissionsHint[4];
//                    break;
//                case CODE_ACCESS_FINE_LOCATION:
////                获取精确的位置
//                    content = permissionsHint[5];
//                    break;
//                case CODE_ACCESS_COARSE_LOCATION:
////                获取粗略的位置
//                    content = permissionsHint[6];
//                    break;
//                case CODE_READ_EXTERNAL_STORAGE:
////                读取sdk
//                    content = content + permissionsHint[7];
//                    break;
//                case CODE_WRITE_EXTERNAL_STORAGE:
////                写入sdk
//                    content = permissionsHint[8];
//                    break;
//            }
//        }
//        return content;
//    }
//}

package com.lzz.bottomencapsulation.utils;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;

import com.lzz.bottomencapsulation.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 权限获取
 */
public class PermissionUtils {

    private static final String TAG = PermissionUtils.class.getSimpleName();

    public static final int CODE_RECORD_AUDIO = 0;
    //    public static final int CODE_GET_ACCOUNTS = 1;
    public static final int CODE_READ_PHONE_STATE = 1;
    //  public static final int CODE_CALL_PHONE = 3;
//  public static final int CODE_CAMERA = 4;
//  public static final int CODE_ACCESS_FINE_LOCATION = 5;
//  public static final int CODE_ACCESS_COARSE_LOCATION = 6;
    public static final int CODE_READ_EXTERNAL_STORAGE = 2;
    public static final int CODE_WRITE_EXTERNAL_STORAGE = 3;
    /**
     * 权限全部授予
     */
    public static final int CODE_MULTI_PERMISSION = 100;
    /**
     * 取消权限授予
     */
    public static final int CODE_CANCLE = -1;
    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    //  public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    //  public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
//  public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
//  public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission
//            .ACCESS_FINE_LOCATION;
//  public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission
//            .ACCESS_COARSE_LOCATION;
    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission
            .READ_EXTERNAL_STORAGE;
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission
            .WRITE_EXTERNAL_STORAGE;

    private static final String[] pushLivePermissions = {PERMISSION_RECORD_AUDIO,
            //            PERMISSION_GET_ACCOUNTS,
            PERMISSION_READ_PHONE_STATE,
            //            PERMISSION_CALL_PHONE,
            //            PERMISSION_CAMERA,
            //            PERMISSION_ACCESS_FINE_LOCATION,
            //            PERMISSION_ACCESS_COARSE_LOCATION,
            PERMISSION_READ_EXTERNAL_STORAGE, PERMISSION_WRITE_EXTERNAL_STORAGE};

    public interface PermissionGrant {
        void onPermissionGranted(int requestCode);
    }

    /**
     * Requests permission.
     *
     * @param activity
     * @param requestCode request code, e.g. if you need request CAMERA permission,parameters is
     *                    PermissionUtils
     *                    .CODE_CAMERA
     */
    public static void requestPermission(final Activity activity, final int requestCode,
                                         PermissionGrant permissionGrant) {
        if (activity == null) {
            return;
        }

        Log.i(TAG, "requestPermission requestCode:" + requestCode);
        if (requestCode < 0 || requestCode >= pushLivePermissions.length) {
            Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
            return;
        }

        final String requestPermission = pushLivePermissions[requestCode];

        //如果是6.0以下的手机，ActivityCompat.checkSelfPermission()会始终等于PERMISSION_GRANTED，
        // 但是，如果用户关闭了你申请的权限，ActivityCompat.checkSelfPermission(),会导致程序崩溃(java.lang
        // .RuntimeException: Unknown
        // exception code: 1 msg null)，
        // 你可以使用try{}catch(){},处理异常，也可以判断系统版本，低于23就不申请权限，直接做你想做的。permissionGrant
        // .onPermissionGranted(requestCode);
        if (Build.VERSION.SDK_INT < 23) {
            permissionGrant.onPermissionGranted(requestCode);
            return;
        }

        int checkSelfPermission;
        try {
            checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
        } catch (RuntimeException e) {
            Toast.makeText(activity, "请开启您的权限", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "RuntimeException:" + e.getMessage());
            return;
        }

        if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
            Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");


            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission)) {
                Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
                shouldShowRationale(activity, requestCode, requestPermission, permissionGrant);

            } else {
                Log.d(TAG, "requestCameraPermission else");
                ActivityCompat.requestPermissions(activity, new String[]{requestPermission},
                        requestCode);
            }

        } else {
            Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager " + "" + "" +
                    ".PERMISSION_GRANTED");

            permissionGrant.onPermissionGranted(requestCode);
        }
    }

    private static void requestMultiResult(Activity activity, String[] permissions, int[]
            grantResults, PermissionGrant permissionGrant) {

        if (activity == null) {
            return;
        }

        Log.d(TAG, "onRequestPermissionsResult permissions length:" + permissions.length);
        Map<String, Integer> perms = new HashMap<>();

        ArrayList<String> notGranted = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            Log.d(TAG, "permissions: [i]:" + i + ", permissions[i]" + permissions[i] + "," +
                    "grantResults[i]:" + grantResults[i]);
            perms.put(permissions[i], grantResults[i]);
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                notGranted.add(permissions[i]);
            }
        }

        if (notGranted.size() == 0) {
            //            权限获取成功
            permissionGrant.onPermissionGranted(CODE_MULTI_PERMISSION);
        } else {
            //            权限未授予
            openSettingActivity(activity, activity.getResources().getString(R.string
                    .ApplicationInformation), permissionGrant);
        }

    }


    /**
     * 一次申请多个权限
     */
    public static void requestMultiPermissions(final Activity activity, final PermissionGrant
            grant) {
        /**
         * 不需要授予权限说明，暂未授予的权限
         */
        final List<String> permissionsList = getNoGrantedPermission(activity, false);
        /**
         * 需要授予说明，暂未授予的权限
         */
        final List<String> shouldRationalePermissionsList = getNoGrantedPermission(activity, true);

        if (permissionsList == null || shouldRationalePermissionsList == null) {
            return;
        }

        if (permissionsList.size() > 0) {
            ActivityCompat.requestPermissions(activity, permissionsList.toArray(new
                    String[permissionsList.size()]), CODE_MULTI_PERMISSION);
        } else if (shouldRationalePermissionsList.size() > 0) {
            showMessageOKCancel(activity, activity.getResources().getString(R.string
                    .notPermission), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions(activity, shouldRationalePermissionsList
                            .toArray(new String[shouldRationalePermissionsList.size()]),
                            CODE_MULTI_PERMISSION);
                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //                            取消授予权限
                    grant.onPermissionGranted(CODE_CANCLE);
                }
            });
        } else {
            //            权限全部授予
            grant.onPermissionGranted(CODE_MULTI_PERMISSION);
        }

    }


    private static void shouldShowRationale(final Activity activity, final int requestCode, final
    String requestPermission, final PermissionGrant permissionGrant) {
        String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
        showMessageOKCancel(activity, permissionsHint[requestCode], new DialogInterface
                .OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(activity, new String[]{requestPermission},
                        requestCode);
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                permissionGrant.onPermissionGranted(CODE_CANCLE);
            }
        });
    }

    private static void showMessageOKCancel(final Activity context, String message,
                                            DialogInterface.OnClickListener okListener,
                                            DialogInterface.OnClickListener noListener) {
        Log.e("弹出框", "弹出框");
        AlertDialog alertDialog = new AlertDialog.Builder(context).setMessage(message)
                .setPositiveButton(context.getResources().getString(R.string.confirm),
                        okListener).setNegativeButton(context.getResources().getString(R.string
                        .cancel), noListener).create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.setCancelable(false);
        alertDialog.show();


    }

    /**
     * @param activity
     * @param requestCode  Need consistent with requestPermission
     * @param permissions
     * @param grantResults
     */
    public static void requestPermissionsResult(final Activity activity, final int requestCode,
                                                @NonNull String[] permissions, @NonNull int[]
                                                        grantResults, PermissionGrant
                                                        permissionGrant) {

        if (activity == null) {
            return;
        }

        if (requestCode == CODE_MULTI_PERMISSION) {
            requestMultiResult(activity, permissions, grantResults, permissionGrant);
            return;
        }

        if (requestCode < 0 || requestCode >= pushLivePermissions.length) {
            return;
        }

        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            permissionGrant.onPermissionGranted(requestCode);

        } else {
            String[] permissionsHint = activity.getResources().getStringArray(R.array.permissions);
            openSettingActivity(activity, permissionsHint[requestCode], permissionGrant);
        }

    }

    private static void openSettingActivity(final Activity activity, String message, final
    PermissionGrant permissionGrant) {

        showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                intent.setData(uri);
                activity.startActivity(intent);
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                permissionGrant.onPermissionGranted(CODE_CANCLE);
            }
        });
    }


    /**
     * 获得未授予的权限
     *
     * @param activity
     * @param isShouldRationale true: return no granted and shouldShowRequestPermissionRationale
     *                          permissions,
     *                          false:return no granted and !shouldShowRequestPermissionRationale
     * @return
     */
    public static ArrayList<String> getNoGrantedPermission(Activity activity, boolean
            isShouldRationale) {

        ArrayList<String> permissions = new ArrayList<>();

        for (int i = 0; i < pushLivePermissions.length; i++) {
            String requestPermission = pushLivePermissions[i];
            int checkSelfPermission = -1;
            try {
                checkSelfPermission = ActivityCompat.checkSelfPermission(activity,
                        requestPermission);
            } catch (RuntimeException e) {
                //                获取权限情况出现异常
                TUtils.showToast(activity, activity.getResources().getString(R.string
                        .notPermission));
                Log.e(TAG, "RuntimeException:" + e.getMessage());
                return null;
            }
            if (checkSelfPermission != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                        requestPermission)) {
                    if (isShouldRationale) {
                        permissions.add(requestPermission);
                    }
                } else {
                    if (!isShouldRationale) {
                        permissions.add(requestPermission);
                    }
                }
            }
        }
        return permissions;
    }

}
